package com.leap.meeting.dao.imple;

import com.leap.meeting.dao.BaseDAO;
import com.leap.meeting.dao.RoomDAO;
import com.leap.meeting.pojo.Room;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.List;

public class RoomDAOimple extends BaseDAO implements RoomDAO {
    @Override
    public List<Room> selectAll() {
        try {
            String sql = "SELECT id,name,address,seat_num AS seatNum,status from rooms";
            return super.executeQuery(Room.class, sql, null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Room selectByName(String name) {
        try {
            String sql = "SELECT id,name,address,seat_num AS seatNum,status from rooms where name = ?";
            return super.executeQuerySingle(Room.class, sql, name);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Room selectById(int id) {
        try {
            String sql = "SELECT id,name,address,seat_num AS seatNum,status from rooms where id = ?";
            return super.executeQuerySingle(Room.class, sql, id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    @Override
    public int update(Room room) {
        String sql = "UPDATE rooms SET name = ? , address = ? , seat_num = ? , status = ? WHERE id = ?";
        return super.executeUpdate(sql, room.getName(),room.getAddress(),room.getSeatNum(),room.getStatus(),room.getId());
    }

    @Override
    public int insert(Room room) {
        String sql = "INSERT INTO rooms(name, address, seat_num, status) VALUES (?,?,?,'空闲')";
        return super.executeUpdate(sql, room.getName(), room.getAddress(), room.getSeatNum());
    }
}
